/**
 * TB - 主界面
 */
Ext.define("PSI.Sale.SRMainForm", {
  extend: "PSI.AFX.BaseMainExForm",

  config: {
    permission: null
  },

  initComponent: function () {
    var me = this;

    Ext.apply(me, {
      tbar: me.getToolbarCmp(),
      items: [{
        id: "panelQueryCmp",
        region: "north",
        height: 95,
        layout: "fit",
        border: 0,
        header: false,
        collapsible: true,
        collapseMode: "mini",
        hidden:false,
        layout: {
          type: "table",
          columns: 4
        },
        items: me.getQueryCmp()
      }, {
        region: "center",
        layout: "border",
        border: 0,
        items: [{
          region: "north",
          height: "90%",
          split: true,
          layout: "fit",
          border: 0,
          items: [me.getMainGrid()]
        }, {
          region: "center",
          layout: "fit",
          border: 0,
          items: [me.getDetailGrid()],
          hidden:true
        }]
      }]
    });

    me.callParent(arguments);

    me.refreshMainGrid();
  },

  getToolbarCmp: function () {
    var me = this;
    return [{
    //   text: "新建TB单",
    //   hidden: me.getPermission().add == "0",
    //   id: "buttonAdd",
    //   scope: me,
    //   handler: me.onAddBill
    // }, {
    //   hidden: me.getPermission().add == "0",
    //   xtype: "tbseparator"
    // }, {
    //   hidden: me.getPermission().edit == "0",
    //   text: "编辑TB单",
    //   id: "buttonEdit",
    //   scope: me,
    //   handler: me.onEditBill
    // }, {
    //   hidden: me.getPermission().edit == "0",
    //   xtype: "tbseparator"
    // }, {
    //   text: "删除TB单",
    //   hidden: me.getPermission().del == "0",
    //   id: "buttonDelete",
    //   scope: me,
    //   handler: me.onDeleteBill
    // }, {
    //   hidden: me.getPermission().del == "0",
    //   xtype: "tbseparator"
    // }, {
    //   hidden: me.getPermission().commit == "0",
    //   text: "提交入库",
    //   id: "buttonCommit",
    //   scope: me,
    //   handler: me.onCommit
    // }, {
    //   hidden: me.getPermission().commit == "0",
    //   xtype: "tbseparator"
    // }, {
      text: "导出",
      hidden: me.getPermission().genPDF == "0",
      menu: [{
        text: "单据生成pdf",
        iconCls: "PSI-button-pdf",
        id: "buttonPDF",
        scope: me,
        handler: me.onPDF
      }]
    }, {
      hidden: me.getPermission().genPDF == "0",
      xtype: "tbseparator"
    }, {
      text: "打印",
      hidden: me.getPermission().print == "0",
      menu: [{
        text: "打印预览",
        iconCls: "PSI-button-print-preview",
        scope: me,
        handler: me.onPrintPreview
      }, "-", {
        text: "直接打印",
        iconCls: "PSI-button-print",
        scope: me,
        handler: me.onPrint
      }]
    }, {
      xtype: "tbseparator",
      hidden: me.getPermission().print == "0"
    }, {
      text: "帮助",
      handler: function () {
        window.open(me.URL("Home/Help/index?t=srbill"));
      }
    }, "-", {
      text: "关闭",
      handler: function () {
        me.closeWindow();
      }
    }];
  },

  getQueryCmp: function () {
    var me = this;
    return [{
      id: "editQuerySeller_nick",
      xtype: "combo",
      queryMode: "local",
      editable: false,
      valueField: "id",
      labelWidth: 60,
      labelAlign: "right",
      labelSeparator: "",
      fieldLabel: "店铺",
      margin: "5, 0, 0, 0",
      store: Ext.create("Ext.data.ArrayStore", {
        fields: ["id", "text"],
        data: [[-1, "全部"], ['伊安迪旗舰店', "伊安迪"], ['帝成个人护理旗舰店', "帝成"], ['康多力旗舰店', "康多力"], ['gax旗舰店', "GAX"]]
      }),
      value: -1
    }, {
      id: "editQueryFromcreated",
      xtype: "datefield",
      margin: "5, 0, 0, 0",
      format: "Y-m-d",
      labelAlign: "right",
      labelSeparator: "",
      fieldLabel: "交易日期（起）"
    }, {
      id: "editQueryTocreated",
      xtype: "datefield",
      margin: "5, 0, 0, 0",
      format: "Y-m-d",
      labelAlign: "right",
      labelSeparator: "",
      fieldLabel: "交易日期（止）"
    }, {
      id: "editQueryTid",
      labelWidth: 60,
      labelAlign: "right",
      labelSeparator: "",
      fieldLabel: "订单号",
      margin: "5, 0, 0, 0",
      xtype: "textfield"
     }, {
    //   id: "editQueryCustomer",
    //   xtype: "psi_customerfield",
    //   showModal: true,
    //   labelAlign: "right",
    //   labelSeparator: "",
    //   labelWidth: 60,
    //   margin: "5, 0, 0, 0",
    //   fieldLabel: "客户"
    // }, {
    //   id: "editQueryWarehouse",
    //   xtype: "psi_warehousefield",
    //   showModal: true,
    //   labelAlign: "right",
    //   labelSeparator: "",
    //   labelWidth: 60,
    //   margin: "5, 0, 0, 0",
    //   fieldLabel: "仓库"
    // }, {
    //   id: "editQuerySN",
    //   labelAlign: "right",
    //   labelSeparator: "",
    //   fieldLabel: "序列号",
    //   margin: "5, 0, 0, 0",
    //   xtype: "textfield"
    // }, {
    //   id: "editQueryPaymentType",
    //   labelAlign: "right",
    //   labelSeparator: "",
    //   fieldLabel: "付款方式",
    //   margin: "5, 0, 0, 0",
    //   xtype: "combo",
    //   queryMode: "local",
    //   editable: false,
    //   valueField: "id",
    //   store: Ext.create("Ext.data.ArrayStore", {
    //     fields: ["id", "text"],
    //     data: [[-1, "全部"], [0, "记应付账款"], [1, "现金付款"],
    //     [2, "退款转入预收款"]]
    //   }),
    //   value: -1
    // }, {
      id: "editQueryGoods",
      labelAlign: "right",
      labelSeparator: "",
      fieldLabel: "商品",
      labelWidth: 60,
      hidden:true,
      margin: "5, 0, 0, 0",
      xtype: "psi_goodsfield",
      showModal: true
    }, {
      xtype: "container",
      items: [{
        xtype: "button",
        text: "查询",
        width: 100,
        height: 26,
        margin: "5 0 0 10",
        handler: me.onQuery,
        scope: me
      }, {
        xtype: "button",
        text: "清空查询条件",
        width: 100,
        height: 26,
        margin: "5, 0, 0, 10",
        handler: me.onClearQuery,
        scope: me
      }]
    }, {
      xtype: "container",
      items: [{
        xtype: "button",
        text: "隐藏查询条件栏",
        width: 130,
        height: 26,
        iconCls: "PSI-button-hide",
        margin: "5 0 0 10",
        handler: function () {
          Ext.getCmp("panelQueryCmp").collapse();
        },
        scope: me
      }]
    }];
  },

  refreshMainGrid: function (id) {
    var me = this;

    // Ext.getCmp("buttonEdit").setDisabled(true);
    // Ext.getCmp("buttonDelete").setDisabled(true);
    // Ext.getCmp("buttonCommit").setDisabled(true);

    var gridDetail = this.getDetailGrid();
    gridDetail.setTitle(me.formatGridHeaderTitle("TB单明细"));
    gridDetail.getStore().removeAll();
    Ext.getCmp("pagingToobar").doRefresh();
    this.__lastId = id;
  },

  // // 新增TB单
  // onAddBill: function () {
  //   var form = Ext.create("PSI.Sale.SREditForm", {
  //     parentForm: this
  //   });
  //   form.show();
  // },
  //
  // // 编辑TB单
  // onEditBill: function () {
  //   var me = this;
  //   var item = me.getMainGrid().getSelectionModel().getSelection();
  //   if (item == null || item.length != 1) {
  //     PSI.MsgBox.showInfo("请选择要编辑的TB单");
  //     return;
  //   }
  //   var bill = item[0];
  //
  //   var form = Ext.create("PSI.Sale.SREditForm", {
  //     parentForm: me,
  //     entity: bill
  //   });
  //   form.show();
  // },
  //
  // // 删除TB单
  // onDeleteBill: function () {
  //   var me = this;
  //   var item = me.getMainGrid().getSelectionModel().getSelection();
  //   if (item == null || item.length != 1) {
  //     PSI.MsgBox.showInfo("请选择要删除的TB单");
  //     return;
  //   }
  //   var bill = item[0];
  //
  //   if (bill.get("billStatus") == "已入库") {
  //     PSI.MsgBox.showInfo("当前TB单已经提交入库，不能删除");
  //     return;
  //   }
  //
  //   var info = "请确认是否删除TB单: <span style='color:red'>" + bill.get("ref")
  //     + "</span>";
  //   PSI.MsgBox.confirm(info, function () {
  //     var el = Ext.getBody();
  //     el.mask("正在删除中...");
  //     Ext.Ajax.request({
  //       url: PSI.Const.BASE_URL
  //         + "Home/Sale/deleteSRBill",
  //       method: "POST",
  //       params: {
  //         id: bill.get("id")
  //       },
  //       callback: function (options, success, response) {
  //         el.unmask();
  //
  //         if (success) {
  //           var data = Ext.JSON
  //             .decode(response.responseText);
  //           if (data.success) {
  //             PSI.MsgBox.showInfo("成功完成删除操作",
  //               function () {
  //                 me.refreshMainGrid();
  //               });
  //           } else {
  //             PSI.MsgBox.showInfo(data.msg);
  //           }
  //         } else {
  //           PSI.MsgBox.showInfo("网络错误", function () {
  //             window.location.reload();
  //           });
  //         }
  //       }
  //     });
  //   });
  // },
  //
  // // 提交TB单
  // onCommit: function () {
  //   var me = this;
  //   var item = me.getMainGrid().getSelectionModel().getSelection();
  //   if (item == null || item.length != 1) {
  //     PSI.MsgBox.showInfo("没有选择要提交的TB单");
  //     return;
  //   }
  //   var bill = item[0];
  //
  //   if (bill.get("billStatus") == "已入库") {
  //     PSI.MsgBox.showInfo("当前TB单已经提交入库，不能再次提交");
  //     return;
  //   }
  //
  //   var info = "请确认是否提交单号: <span style='color:red'>" + bill.get("ref")
  //     + "</span> 的TB单?";
  //   PSI.MsgBox.confirm(info, function () {
  //     var el = Ext.getBody();
  //     el.mask("正在提交中...");
  //     Ext.Ajax.request({
  //       url: PSI.Const.BASE_URL
  //         + "Home/Sale/commitSRBill",
  //       method: "POST",
  //       params: {
  //         id: bill.get("id")
  //       },
  //       callback: function (options, success, response) {
  //         el.unmask();
  //
  //         if (success) {
  //           var data = Ext.JSON
  //             .decode(response.responseText);
  //           if (data.success) {
  //             PSI.MsgBox.showInfo("成功完成提交操作",
  //               function () {
  //                 me
  //                   .refreshMainGrid(data.id);
  //               });
  //           } else {
  //             PSI.MsgBox.showInfo(data.msg);
  //           }
  //         } else {
  //           PSI.MsgBox.showInfo("网络错误", function () {
  //             window.location.reload();
  //           });
  //         }
  //       }
  //     });
  //   });
  // },

  getMainGrid: function () {
    var me = this;
    if (me.__mainGrid) {
      return me.__mainGrid;
    }

    var modelName = "PSISRBill";
    Ext.define(modelName, {
      extend: "Ext.data.Model",
      fields: ["id", "adjust_fee", "buyer_nick", "buyer_rate","cid", "consign_time", "create_time","created", "discount_fee", "divide_order_fee",
        "end_time", "has_buyer_message", "seller_nick", "invoice_no","is_daixiao", "issd", "logistics_company","modified", "no_shipping", "num","num_iid","o2o_guide_id",
        "o2o_guide_name", "oid", "outer_sku_id","part_mjz_discount","pay_time","payment","price","receiver_mobile","receiver_name","receiver_phone","refund_status",
        "sdtime","seller_rate","seller_type","shipping_type","sku_id","sku_properties_name","status","tid","title","total_fee","type","update_time"
        ]
    });
    var store = Ext.create("Ext.data.Store", {
      autoLoad: false,
      model: modelName,
      data: [],
      pageSize: 20,
      proxy: {
        type: "ajax",
        actionMethods: {
          read: "POST"
        },
        url: PSI.Const.BASE_URL + "Home/Taobaoorder/tbbillList",
        reader: {
          root: 'dataList',
          totalProperty: 'totalCount'
        }
      }
    });
    store.on("beforeload", function () {
      store.proxy.extraParams = me.getQueryParam();
    });
    store.on("load", function (e, records, successful) {
      if (successful) {
        me.gotoMainGridRecord(me.__lastId);
      }
    });

    me.__mainGrid = Ext.create("Ext.grid.Panel", {
      cls: "PSI",
      viewConfig: {
        enableTextSelection: true
      },
      border: 1,
      columnLines: true,
      columns: [{
        xtype: "rownumberer",
        width: 50
      }, {
        header: "刷单",
        dataIndex: "issd",
        menuDisabled: true,
        sortable: false,
        width: 50,
        renderer: function (value) {
          return value == 1
            ? "<span style='color:red'> 是</span>"
            : '否';
        }
      }, {
        header: "改价",
        dataIndex: "adjust_fee",
        width: 60,
        menuDisabled: true,
        sortable: false
      }, {
        header: "买家昵称",
        dataIndex: "buyer_nick",
        menuDisabled: true,
        sortable: false
      }, {
        header: "评价",
        dataIndex: "buyer_rate",
        width: 50,
        menuDisabled: true,
        sortable: false
      }, {
        header: "商品类目",
        dataIndex: "cid",
        menuDisabled: true,
        sortable: false,
        align: "right",
        // xtype: "numbercolumn",
        width: 80
      }, {
        header: "发货时间",
        dataIndex: "consign_time",
        menuDisabled: true,
        sortable: false,
        align: "right",
        width: 120
      }, {
        header: "首次同步时间",
        dataIndex: "create_time",
        menuDisabled: true,
        sortable: false,
        align: "right",
        width: 120
      }, {
        header: "交易创建时间",
        dataIndex: "created",
        menuDisabled: true,
        sortable: false,
        width: 100
      }, {
        header: "优惠系统",
        dataIndex: "discount_fee",
        menuDisabled: true,
        sortable: false,
        width: 50
      }, {
        header: "实付金额",
        dataIndex: "divide_order_fee",
        menuDisabled: true,
        sortable: false,
        width: 60
      }, {
        header: "交易结束时间",
        dataIndex: "end_time",
        menuDisabled: true,
        sortable: false
      }, {
        header: "是否留言",
        dataIndex: "has_buyer_message",
        width: 80,
        menuDisabled: true,
        sortable: false
      },{
        header: "卖家",
        dataIndex: "seller_nick",
        menuDisabled: true,
        sortable: false
      }, {
        header: "子运单号",
        dataIndex: "invoice_no",
        width: 120,
        menuDisabled: true,
        sortable: false
      }, {
        header: "代销单?",
        dataIndex: "is_daixiao",
        width: 60,
        menuDisabled: true,
        sortable: false
      }, {
        header: "子单快递",
        dataIndex: "logistics_company",
        width: 80,
        menuDisabled: true,
        sortable: false
      }, {
        header: "交易修改",
        dataIndex: "modified",
        width: 130,
        menuDisabled: true,
        sortable: false
      }, {
        header: "无物流",
        dataIndex: "no_shipping",
        width: 50,
        menuDisabled: true,
        sortable: false
      }, {
        header: "买数量",
        dataIndex: "num",
        width: 60,
        menuDisabled: true,
        sortable: false
      }, {
        header: "商品编号",
        dataIndex: "num_iid",
        width: 100,
        menuDisabled: true,
        sortable: false
      }, {
        header: "导购id",
        dataIndex: "o2o_guide_id",
        width: 60,
        menuDisabled: true,
        sortable: false
      }, {
        header: "导购名",
        dataIndex: "o2o_guide_name",
        width: 80,
        menuDisabled: true,
        sortable: false
      }, {
        header: "子单号",
        dataIndex: "oid",
        width: 150,
        menuDisabled: true,
        sortable: false
      }, {
        header: "Sku",
        dataIndex: "outer_sku_id",
        width: 120,
        menuDisabled: true,
        sortable: false
      }, {
        header: "优惠分摊",
        dataIndex: "part_mjz_discount",
        width: 80,
        menuDisabled: true,
        sortable: false
      }, {
        header: "付款时间",
        dataIndex: "pay_time",
        width: 130,
        menuDisabled: true,
        sortable: false
      }, {
        header: "子单实付",
        dataIndex: "payment",
        width: 80,
        menuDisabled: true,
        sortable: false
      }, {
        header: "商品价格",
        dataIndex: "price",
        width: 80,
        menuDisabled: true,
        sortable: false
      }, {
        header: "收货人手机",
        dataIndex: "receiver_mobile",
        width: 90,
        menuDisabled: true,
        sortable: false
      }, {
        header: "收货人姓名",
        dataIndex: "receiver_name",
        width: 80,
        menuDisabled: true,
        sortable: false
      }, {
        header: "收货人电话",
        dataIndex: "receiver_phone",
        width: 80,
        menuDisabled: true,
        sortable: false
      }, {
        header: "退款状态",
        dataIndex: "refund_status",
        width: 100,
        menuDisabled: true,
        sortable: false
      }, {
        header: "刷单时间",
        dataIndex: "sdtime",
        width: 120,
        menuDisabled: true,
        sortable: false
      }, {
        header: "卖家评价",
        dataIndex: "seller_rate",
        width: 60,
        menuDisabled: true,
        sortable: false
      }, {
        header: "卖家类型",
        dataIndex: "seller_type",
        width: 80,
        menuDisabled: true,
        sortable: false
      }, {
        header: "子单运送方式",
        dataIndex: "shipping_type",
        width: 100,
        menuDisabled: true,
        sortable: false
      }, {
        header: "SKU_id",
        dataIndex: "sku_id",
        width: 120,
        menuDisabled: true,
        sortable: false
      }, {
        header: "SKU值",
        dataIndex: "sku_properties_name",
        width: 300,
        menuDisabled: true,
        sortable: false
      }, {
        header: "订单状态",
        dataIndex: "status",
        width: 130,
        menuDisabled: true,
        sortable: false
      }, {
        header: "交易编号",
        dataIndex: "tid",
        width: 150,
        menuDisabled: true,
        sortable: false
      }, {
        header: "交易标题",
        dataIndex: "title",
        width: 150,
        menuDisabled: true,
        sortable: false
      }, {
        header: "总金额",
        dataIndex: "total_fee",
        width: 60,
        menuDisabled: true,
        sortable: false
      }, {
        header: "交易类型",
        dataIndex: "type",
        width: 80,
        menuDisabled: true,
        sortable: false
      }],
      listeners: {
        select: {
          // fn: me.onMainGridSelect,
          scope: me
        },
        itemdblclick: {
          fn: me.getPermission().edit == "1"
            ? me.onEditBill
            : Ext.emptyFn,
          scope: me
        }
      },
      store: store,
      bbar: ["->", {
        id: "pagingToobar",
        xtype: "pagingtoolbar",
        border: 0,
        store: store
      }, "-", {
          xtype: "displayfield",
          value: "每页显示"
        }, {
          id: "comboCountPerPage",
          xtype: "combobox",
          editable: false,
          width: 60,
          store: Ext.create("Ext.data.ArrayStore", {
            fields: ["text"],
            data: [["20"], ["50"], ["100"],
            ["300"], ["1000"]]
          }),
          value: 20,
          listeners: {
            change: {
              fn: function () {
                store.pageSize = Ext
                  .getCmp("comboCountPerPage")
                  .getValue();
                store.currentPage = 1;
                Ext.getCmp("pagingToobar")
                  .doRefresh();
              },
              scope: me
            }
          }
        }, {
          xtype: "displayfield",
          value: "条记录"
        }]
    });

    return me.__mainGrid;
  },

  getDetailGrid: function () {
    var me = this;
    if (me.__detailGrid) {
      return me.__detailGrid;
    }

    var modelName = "PSISRBillDetail";
    Ext.define(modelName, {
      extend: "Ext.data.Model",
      fields: ["id", "goodsCode", "goodsName", "goodsSpec",
        "unitName", "goodsCount", "goodsMoney",
        "goodsPrice", "rejCount", "rejPrice",
        "rejSaleMoney", "sn", "memo", "taxRate", "tax",
        "rejSaleMoneyWithTax", "rejPriceWithTax"]
    });
    var store = Ext.create("Ext.data.Store", {
      autoLoad: false,
      model: modelName,
      data: []
    });

    me.__detailGrid = Ext.create("Ext.grid.Panel", {
      cls: "PSI",
      viewConfig: {
        enableTextSelection: true
      },
      header: {
        height: 30,
        title: me.formatGridHeaderTitle("TB单明细")
      },
      columnLines: true,
      columns: [Ext.create("Ext.grid.RowNumberer", {
        text: "序号",
        width: 40
      }), {
        header: "商品编码",
        dataIndex: "goodsCode",
        menuDisabled: true,
        sortable: false,
        width: 120
      }, {
        header: "商品名称",
        dataIndex: "goodsName",
        menuDisabled: true,
        sortable: false,
        width: 200
      }, {
        header: "规格型号",
        dataIndex: "goodsSpec",
        menuDisabled: true,
        sortable: false,
        width: 200
      }, {
        header: "退货数量",
        dataIndex: "rejCount",
        menuDisabled: true,
        sortable: false,
        align: "right"
      }, {
        header: "单位",
        dataIndex: "unitName",
        menuDisabled: true,
        sortable: false,
        width: 60
      }, {
        header: "退货单价(不含税)",
        dataIndex: "rejPrice",
        menuDisabled: true,
        sortable: false,
        align: "right",
        xtype: "numbercolumn",
        width: 150
      }, {
        header: "退货金额(不含税)",
        dataIndex: "rejSaleMoney",
        menuDisabled: true,
        sortable: false,
        align: "right",
        xtype: "numbercolumn",
        width: 150
      }, {
        header: "税率(%)",
        dataIndex: "taxRate",
        menuDisabled: true,
        sortable: false,
        align: "right",
        xtype: "numbercolumn",
        format: "#",
        width: 80
      }, {
        header: "税金(红字)",
        dataIndex: "tax",
        menuDisabled: true,
        sortable: false,
        align: "right",
        xtype: "numbercolumn",
        width: 150
      }, {
        header: "价税合计",
        dataIndex: "rejSaleMoneyWithTax",
        menuDisabled: true,
        sortable: false,
        align: "right",
        xtype: "numbercolumn",
        width: 150
      }, {
        header: "含税价",
        dataIndex: "rejPriceWithTax",
        menuDisabled: true,
        sortable: false,
        align: "right",
        xtype: "numbercolumn",
        width: 150
      }, {
        header: "序列号",
        dataIndex: "sn",
        menuDisabled: true,
        sortable: false
      }, {
        header: "备注",
        dataIndex: "memo",
        width: 200,
        menuDisabled: true,
        sortable: false
      }],
      store: store
    });

    return me.__detailGrid;
  },

  gotoMainGridRecord: function (id) {
    var me = this;
    var grid = me.getMainGrid();
    grid.getSelectionModel().deselectAll();
    var store = grid.getStore();
    if (id) {
      var r = store.findExact("id", id);
      if (r != -1) {
        grid.getSelectionModel().select(r);
      } else {
        grid.getSelectionModel().select(0);
      }
    } else {
      grid.getSelectionModel().select(0);
    }
  },

  onMainGridSelect: function () {
    var me = this;
    me.getDetailGrid().setTitle(me.formatGridHeaderTitle("TB单明细"));
    var grid = me.getMainGrid();
    var item = grid.getSelectionModel().getSelection();
    if (item == null || item.length != 1) {
      Ext.getCmp("buttonEdit").setDisabled(true);
      Ext.getCmp("buttonDelete").setDisabled(true);
      Ext.getCmp("buttonCommit").setDisabled(true);
      return;
    }
    var bill = item[0];

    var commited = bill.get("billStatus") == "已入库";

    var buttonEdit = Ext.getCmp("buttonEdit");
    buttonEdit.setDisabled(false);
    if (commited) {
      buttonEdit.setText("查看TB单");
    } else {
      buttonEdit.setText("编辑TB单");
    }

    Ext.getCmp("buttonDelete").setDisabled(commited);
    Ext.getCmp("buttonCommit").setDisabled(commited);

    me.freshDetailGrid();
  },

  freshDetailGrid: function (id) {
    var me = this;
    me.getDetailGrid().setTitle(me.formatGridHeaderTitle("TB单明细"));
    var grid = me.getMainGrid();
    var item = grid.getSelectionModel().getSelection();
    if (item == null || item.length != 1) {
      return;
    }
    var bill = item[0];

    grid = me.getDetailGrid();
    grid.setTitle(me.formatGridHeaderTitle("单号: " + bill.get("ref")
      + " 客户: " + bill.get("customerName") + " 入库仓库: "
      + bill.get("warehouseName")));
    var el = grid.getEl();
    el.mask(PSI.Const.LOADING);
    Ext.Ajax.request({
      url: PSI.Const.BASE_URL + "Home/Sale/srBillDetailList",
      params: {
        billId: bill.get("id")
      },
      method: "POST",
      callback: function (options, success, response) {
        var store = grid.getStore();

        store.removeAll();

        if (success) {
          var data = Ext.JSON.decode(response.responseText);
          store.add(data);

          if (store.getCount() > 0) {
            if (id) {
              var r = store.findExact("id", id);
              if (r != -1) {
                grid.getSelectionModel().select(r);
              }
            }
          }
        }

        el.unmask();
      }
    });
  },

  onQuery: function () {
    var me = this;

    me.getMainGrid().getStore().currentPage = 1;
    me.refreshMainGrid();
  },

  onClearQuery: function () {
    var me = this;

    Ext.getCmp("editQuerySeller_nick").setValue(-1);
    Ext.getCmp("editQueryTid").setValue(null);
    Ext.getCmp("editQueryFromcreated").setValue(null);
    Ext.getCmp("editQueryTocreated").setValue(null);
    // Ext.getCmp("editQueryCustomer").clearIdValue();
    // Ext.getCmp("editQueryWarehouse").clearIdValue();
    // Ext.getCmp("editQuerySN").setValue(null);
    // Ext.getCmp("editQueryPaymentType").setValue(-1);
    // Ext.getCmp("editQueryGoods").clearIdValue();

    me.onQuery();
  },

  getQueryParam: function () {
    var me = this;

    var result = {
      seller_nick: Ext.getCmp("editQuerySeller_nick").getValue()
    };

    var tid = Ext.getCmp("editQueryTid").getValue();
    if (tid) {
      result.tid = tid;
    }

    // var customerId = Ext.getCmp("editQueryCustomer").getIdValue();
    // if (customerId) {
    //   result.customerId = customerId;
    // }
    //
    // var warehouseId = Ext.getCmp("editQueryWarehouse").getIdValue();
    // if (warehouseId) {
    //   result.warehouseId = warehouseId;
    // }

    var fromcreated = Ext.getCmp("editQueryFromcreated").getValue();
    if (fromcreated) {
      result.fromcreated = Ext.Date.format(fromcreated, "Y-m-d");
    }

    var tocreated = Ext.getCmp("editQueryTocreated").getValue();
    if (tocreated) {
      result.tocreated = Ext.Date.format(tocreated, "Y-m-d");
    }

    // var sn = Ext.getCmp("editQuerySN").getValue();
    // if (sn) {
    //   result.sn = sn;
    // }

    // var paymentType = Ext.getCmp("editQueryPaymentType").getValue();
    // result.paymentType = paymentType;
    //
    // var goodsId = Ext.getCmp("editQueryGoods").getIdValue();
    // if (goodsId) {
    //   result.goodsId = goodsId;
    // }

    return result;
  },

  onPDF: function () {
    var me = this;
    var item = me.getMainGrid().getSelectionModel().getSelection();
    if (item == null || item.length != 1) {
      PSI.MsgBox.showInfo("没有选择要生成pdf文件的TB单");
      return;
    }
    var bill = item[0];

    var url = PSI.Const.BASE_URL + "Home/Sale/srBillPdf?ref="
      + bill.get("ref");
    window.open(url);
  },

	/**
	 * 打印预览
	 */
  onPrintPreview: function () {
    var lodop = getLodop();
    if (!lodop) {
      PSI.MsgBox.showInfo("没有安装Lodop控件，无法打印");
      return;
    }

    var me = this;

    var item = me.getMainGrid().getSelectionModel().getSelection();
    if (item == null || item.length != 1) {
      me.showInfo("没有选择要打印的TB单");
      return;
    }
    var bill = item[0];

    var el = Ext.getBody();
    el.mask("数据加载中...");
    var r = {
      url: PSI.Const.BASE_URL + "Home/Sale/genSRBillPrintPage",
      params: {
        id: bill.get("id")
      },
      callback: function (options, success, response) {
        el.unmask();

        if (success) {
          var data = response.responseText;
          me.previewSRBill(bill.get("ref"), data);
        }
      }
    };
    me.ajax(r);
  },

  PRINT_PAGE_WIDTH: "200mm",
  PRINT_PAGE_HEIGHT: "95mm",

  previewSRBill: function (ref, data) {
    var me = this;

    var lodop = getLodop();
    if (!lodop) {
      PSI.MsgBox.showInfo("Lodop打印控件没有正确安装");
      return;
    }

    lodop.PRINT_INIT("TB单" + ref);
    lodop.SET_PRINT_PAGESIZE(1, me.PRINT_PAGE_WIDTH, me.PRINT_PAGE_HEIGHT,
      "");
    lodop.ADD_PRINT_HTM("0mm", "0mm", "100%", "100%", data);
    var result = lodop.PREVIEW("_blank");
  },

	/**
	 * 直接打印
	 */
  onPrint: function () {
    var lodop = getLodop();
    if (!lodop) {
      PSI.MsgBox.showInfo("没有安装Lodop控件，无法打印");
      return;
    }

    var me = this;

    var item = me.getMainGrid().getSelectionModel().getSelection();
    if (item == null || item.length != 1) {
      me.showInfo("没有选择要打印的TB单");
      return;
    }
    var bill = item[0];

    var el = Ext.getBody();
    el.mask("数据加载中...");
    var r = {
      url: PSI.Const.BASE_URL + "Home/Sale/genSRBillPrintPage",
      params: {
        id: bill.get("id")
      },
      callback: function (options, success, response) {
        el.unmask();

        if (success) {
          var data = response.responseText;
          me.printSRBill(bill.get("ref"), data);
        }
      }
    };
    me.ajax(r);
  },

  printSRBill: function (ref, data) {
    var me = this;

    var lodop = getLodop();
    if (!lodop) {
      PSI.MsgBox.showInfo("Lodop打印控件没有正确安装");
      return;
    }

    lodop.PRINT_INIT("TB单" + ref);
    lodop.SET_PRINT_PAGESIZE(1, me.PRINT_PAGE_WIDTH, me.PRINT_PAGE_HEIGHT,
      "");
    lodop.ADD_PRINT_HTM("0mm", "0mm", "100%", "100%", data);
    var result = lodop.PRINT();
  }
});
